/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openmrs.module.simplelabentry.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.Privilege;
import org.openmrs.api.context.Context;
import org.openmrs.module.hospitalcore.LabService;
import org.openmrs.module.hospitalcore.model.Lab;
import org.openmrs.module.simplelabentry.SimpleLabEntryService;
import org.openmrs.module.simplelabentry.model.OrderModel;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class LabOrderResultController extends SimpleFormController {
    OrderModel model = new OrderModel();
    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        //System.out.println("Inside Form Backing");
        return model;
    }
    private static Log log = LogFactory.getLog(LabOrderReportController.class);

    @Override
    protected Map<String, Object> referenceData(HttpServletRequest request, Object obj, Errors err) throws Exception {
    	SimpleLabEntryService ls = (SimpleLabEntryService) Context.getService(SimpleLabEntryService.class);
        Lab lab = ls.getCurrentLab();
        Map<String, Object> map = new HashMap<String, Object>();
        List<Concept> conceptHeadingList = new ArrayList<Concept>();
        for (Concept concept : lab.getInvestigationsToDisplay()) {
            //System.out.println("_____________________concept = " + concept);
            Privilege p = new Privilege("View " + concept.getName(), "View " + concept + " lab");

            if (Context.getUserService().getPrivilege("View " + concept.getName()) == null) {
                Context.getUserService().savePrivilege(p);
            }
            conceptHeadingList.add(concept);
        }
        Calendar cal = Calendar.getInstance();
        Date today = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        String todayDateStr = sdf.format(today);
        map.put("headingList", conceptHeadingList);
        map.put("todayStr", todayDateStr);
        map.put("lab", ls.getCurrentLab());
        return map;
    }

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
        SimpleLabEntryService ls = (SimpleLabEntryService) Context.getService(SimpleLabEntryService.class);
		Lab lab = ls.getCurrentLab();
    	if( lab == null ){
    		
				log.info("User does not belong to any lab.");
				return new ModelAndView("/module/simplelabentry/configureProperties");
		}
		return super.handleRequestInternal(request, response);
	}

    /*@Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        model = (OrderModel) command;
        System.out.println("Inside OnSubmit = labName " + model.getLabName());
        return new ModelAndView(new RedirectView(request.getContextPath() + getSuccessView()));
    }*/


}
